package com.sgb.kjwl.viewmodel.self.logistics;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.OwnerGoodsDetailInfo;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.self.logistics.OfferOwnerListActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Describe:
 * Author:
 * Date:    2021/8/24
 */
public class OfferOwnerListModel extends AndroidViewModel {
    public MutableLiveData<OwnerGoodsDetailInfo> detailsLiveData = new MutableLiveData<>();

    public OfferOwnerListModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 货源详情
     */
    public void getOwnerGoodsDetailInfo(OfferOwnerListActivity activity, String sourceNo) {
//        DialogHelper.showProgressDialog(activity, null, "加载中", 0, false, null).setCanceledOnTouchOutside(false);
        String isLogin = UserOperating.getInstance().isLogged() ? "true" : "false";
        NewAPIManager.getInstance().getLogisticsAPI().getManagerSourceDetail(isLogin, sourceNo).enqueue(new Callback<BaseEntity<OwnerGoodsDetailInfo>>() {
            @Override
            public void onResponse(Call<BaseEntity<OwnerGoodsDetailInfo>> call, Response<BaseEntity<OwnerGoodsDetailInfo>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    detailsLiveData.postValue(response.body().getData());
                } else if ("201".equals(response.body().getCode()) && response.body().getState().equals("fail")) {
                    MToast.showToast(activity, "货源已被删除或下架");
                } else if (response.body().getState().equals("fail")) {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<OwnerGoodsDetailInfo>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "获取失败");
            }
        });
    }

}
